#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QListWidgetItem>
#include <QDebug>
#include <fstream>
#include "globals.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_searchDialog = new SearchDialog(this);
    this->hide();
    connect(m_searchDialog, SIGNAL(finished(int)), this, SLOT(continueWork()));
    ui->calculateBttn->hide();
    ui->prevBttn->hide();
    m_engine = 0;
    m_settingsDlg = new Settings(this);
    if (!isSettingsCorrect)
        on_settingsBttn_clicked();
    else
        m_searchDialog->show();

    setWindowTitle("WhiteKnight v0.1");
}

void MainWindow::continueWork()
{
    this->loadFile(m_searchDialog->resultFilename());
    m_searchDialog->close();
    this->show();
}

MainWindow::~MainWindow()
{
    dBHandler.close();
    delete ui;
    delete m_settingsDlg;
    delete m_searchDialog;
    delete m_engine;
}

void MainWindow::on_loadGameBttn_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    "Choose game file",
                                                    QApplication::applicationDirPath(),
                                                    "Chess file(*.pgn)");
    loadFile(fileName);
}

void MainWindow::loadFile(QString fileName)
{
    std::ifstream inp;

    try
    {
        inp.open(fileName.toStdString().c_str(), std::ios_base::in);
        inp>>m_games;
    }
    catch (...)
    {
        qDebug("bad");
    }


    for(pgn::GameCollection::iterator it = m_games.begin();
        it!= m_games.end();
        it++)
        {
        QString gameName;

        gameName.append(it->white().c_str());
        gameName+= " ";
        gameName.append(it->black().c_str());
        ui->gameListWidget->addItem(gameName);
    }
    QFile file(fileName);
    QFileInfo inf(file);
    ui->fileName->setText(inf.fileName());
}

void MainWindow::on_nextBttn_clicked()
{
    bool res = m_engine->makeNextHalfMove();
    if(res)
    {
        ui->boardWidget->setTable(m_engine->getVirtualTable());
        ui->gameLogListWidget->setCurrentRow(ui->gameLogListWidget->currentRow()+ (m_halfMove?1:0) );
        m_halfMove = !m_halfMove;
    }
    if (!res)
        ui->gameLogListWidget->setCurrentRow(ui->gameLogListWidget->count() - 1);
}

void MainWindow::on_choosingBttn_clicked()
{
    int ind = ui->gameListWidget->currentIndex();
    if( (ind > -1 ) && (ind < m_games.size()))
    {
        ui->gameLogListWidget->clear();
        pgn::Game currentGame = m_games[ind];
        if ( m_engine != 0)
            delete m_engine;

        m_engine = new ChEngn::Engine(currentGame);
        ui->boardWidget->setTable( m_engine->getVirtualTable());
        m_moves =currentGame.moves();
        for(pgn::MoveList::iterator it = m_moves.begin();
            it != m_moves.end();
            it++)
        {
            ui->gameLogListWidget->addItem(it->toStdString().c_str());
        }
        m_halfMove = false;
    }
}

void MainWindow::on_findBttn_clicked()
{
    m_searchDialog->show();
}

void MainWindow::on_settingsBttn_clicked()
{
    if ( m_settingsDlg != 0)
        m_settingsDlg->showNormal();
}
